from django.shortcuts import render, HttpResponse
from .models import BuddhaWords
from django.core import serializers
from . import models
from django.db.models import Q

from django.views.decorators.csrf import csrf_protect
from django.template import loader

# Create your views here.


def index(request):
    print(request.path)
    list = BuddhaWords.objects.all()
    context = {}
    # context['hello'] = 'Hello World!!!!'
    return render(request, 'home2.html', context)


def index2(request):
    list = BuddhaWords.objects.filter(name='抑郁症')
    data = serializers.serialize("json", list)
    print(data)

    return HttpResponse(data)
    # word = BuddhaWords.objects.get(id=word_id)
    # return HttpResponse(word.name)
    # render(request, 'searchtwo.html', data)
    #全局搜索功能
    # all_buddha = BuddhaWords.objects.all()
    # keyword=request.GET.get('keyword', '')
    # if keyword:
    #     all_buddha = all_buddha.filter(Q(name__icontains=keyword)|Q(desc__icontains=keyword))
    # return render(request, 'searchtwo.html', {'keyword': keyword})

# def search(request):
    # search_name = BuddhaWords.name
    # search_description = BuddhaWords.description
    # search_analysis = BuddhaWords.analysis
    # context = {}
    # context['search_name'] = search_name
    # context['search_description'] = search_description
    # context['search_analysis'] = search_analysis

    # return render(request, 'search.html', context)

def searchtwo(request):
    context = {}
    # if request.method == "GET":
    #     get_id = request.GET.get('name')
    #     if get_id is not None:
    #         try:
    #             db = BuddhaWords.objects.get(id=get_id)
    #             return render(request, 'searchtwo.html', {"data": db})
    #         except:
    #             return render(request, 'searchtwo.html', {'error': "没有找到这样的数据"})
    #     else:
    #         return render(request, 'searchtwo.html', {"error": "传入的数据不可以为空"})
    # else:
    #     return HttpResponse("不可以这样搜索")
    return render(request, 'searchtwo.html', context)


def knowledge(request):
    context = {}
    return render(request, 'knowledge.html', context)

def history(request):
    context = {}
    return render(request, 'history.html', context)

def organization(request):
    context = {}
    return render(request, 'organization.html', context)

def aboutus(request):
    context = {}
    return render(request, 'aboutus.html', context)

def reone(request):
    context = {}
    return render(request, 'reone.html', context)

def retwo(request):
    context = {}
    return render(request, 'retwo.html', context)

def rethree(request):
    context = {}
    return render(request, 'rethree.html', context)


def display(request):
    if('text'):
        search = request.GET.get('text')
        a = BuddhaWords.objects.filter(name__icontains=search)
        #print(a)
        if request.POST:
            p = BuddhaWords(name=request.POST['name'], description=request.POST['description'],
                            analysis=request.POST['analysis'], photo=request.POST['photo'])
            p.save()
        # return getdate(request)
        # HttpResponse(a[0])
        context = {}
        context['word'] = a[0] if a else None
        # HttpResponse(a[0])
        # print(context)
        return render(request, 'searchtwo.html', context)
    else:
        context = {}
        return render(request, 'home2.html', context)

# def getdate(request):
#     list = BuddhaWords.objects.filter()
#     print(list)
#     return render(request, 'searchtwo.html', {'list': list})

